package com.javaclimb.accounting.controller;

import com.javaclimb.accounting.service.FlowService;
import com.javaclimb.accounting.po.FlowBack;
import com.javaclimb.accounting.utils.R;
import com.javaclimb.accounting.vo.CardVo;
import com.javaclimb.accounting.vo.FlowVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;


import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/accounting")
public class FlowController {

    @Autowired
    private FlowService flowService;

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

    //得到所有流水
    @RequestMapping(value = "/flow/getAllFlow",method = RequestMethod.GET)
    public R getAllFlow(FlowVo vo){
        if (vo==null){
            return R.fail("参数有误");
        }
        //拿出前端得到的值
        String userReal = vo.getUserReal();//真实姓名
        long userAccId = vo.getUserAccId();//电子账户id
        String userName=vo.getUserName();//用户名
        String type = vo.getType();//业务类型
        Date startTime = vo.getStartTime();//开始时间
        Date endTime = vo.getEndTime();//结束时间
        String key = vo.getKey();//关键字
        int pageNum = vo.getPageNum();//展示第几页
        int pageSize = vo.getPageSize();//每页展示几条数据
        //返回前端的状态和信息
        String message=null;//错误信息
        try {
                if (endTime == null) {//如果没传结束时间,设置结束时间为当前时间
                    endTime=new Date();
                }
                if (startTime == null) {//如果没传开始时间,设置为结束时间的30天前
                    startTime = flowService.changeTime(endTime, -30);
                }
                //校验差值是否超过三个月
            if (flowService.checkTime(startTime,endTime)>90 || flowService.checkTime(startTime,endTime)<0){

                     message = "时间相差过久";
                     return R.fail(message);
                }
            } catch (Exception e){
            e.printStackTrace();
        }
        //判断下标和展示条数是否正确
        if(pageNum<=0 || pageSize<=0){
            message="下标错误";
            return R.fail(message);
        }
        //拿到数据总条数
        int total=flowService.getTotalNumber(userAccId,userName,type,startTime,endTime,
                key,userReal, pageNum,pageSize);
        //拿到查出来的数据
        List<FlowBack> result=flowService.getAllFlow(userAccId,userName,type,startTime,endTime,
                key,userReal, pageNum,pageSize);


        //遍历查出来的数据
        for (FlowBack flowBack:result){
            //根据充值和提现设置对应值
            if (flowBack.getFlowType().equals("充值")){
                //通过拿到银行卡号和银行名称
                CardVo cardVo=flowService.getCardById(flowBack.getOffAfter());
                //将银行卡号赋值给backNumber,银行名称赋值给bankName
                long bankNumber=Long.parseLong(cardVo.getCardNumber());
                String bankName=cardVo.getCardBank();
                //设置付款方id为银行卡id
                flowBack.setOffBefore(bankNumber);
                //设置付款方名称为银行名称
                flowBack.setBeforeUsername(bankName);
                //设置收款方id为银行卡号
                flowBack.setOffAfter(userAccId);
            }
            if (flowBack.getFlowType().equals("提现")){
                CardVo cardVo=flowService.getCardById(flowBack.getOffAfter());
                long bankNumber=Long.parseLong(cardVo.getCardNumber());
                String bankName=cardVo.getCardBank();


                //设置转账方id为当前用户id
                flowBack.setOffBefore(userAccId);
                //设置收款方名称为银行名称
                flowBack.setAfterUsername(bankName);
                //设置收款方id为银行卡号
                flowBack.setOffAfter(bankNumber);
            }
        }

        HashMap<Object,Object> hashMap=new HashMap<>();
        hashMap.put("Flow",result);
        hashMap.put("total",total);
        return R.ok(hashMap);
    }

}

